import {Token} from "./Token";

const axios = require('axios');

export class Http {
    static get(url: string, params: any): Promise<any> {
        return axios.get(url, {
            params: params,
            headers: {
                "Accept": "*/*",
                "Authorization": Token.getAuthorization()
            }
        });
    }

    static getModel<T>(url: string, params: any={}, headers: any = {
        "Accept": "*/*",
        "Authorization": Token.getAuthorization()
    }): Promise<T> {
        return new Promise<T>((resolve:any,reject:any)=>{
            axios.get(url, {
                params: params,
                headers: headers
            }).then((res:any)=>{
                resolve(res.data);
            }).catch(()=>{
                reject();
            })
        })
    }

    static post(url: string, body: any): Promise<any> {
        return axios.post(url, body, {
            headers: {
                "Accept": "*/*",
                "Authorization": Token.getAuthorization()
            }
        });
    }

    static postModel<T>(url: string, body: any, headers: any = {
        "Accept": "*/*",
        "Authorization": Token.getAuthorization()
    }): Promise<T> {
        return new Promise((resolve:any, reject:any) => {
            axios.post(url, body, {
                headers: headers
            }).then((res: any) => {
                resolve(res.data);
            }).catch((e: any) => {
                reject();
            })
        });
    }

    static websocket(wsUrl: string, option: { onmessage: ((this: WebSocket, ev: MessageEvent) => any) }): WebSocket {
        let ws = new WebSocket(wsUrl);
        if (!!option.onmessage) {
            ws.onmessage = option.onmessage;
        }
        return ws;
    }

    static delete(url: string, body: any, params?: any): Promise<any> {
        return axios.delete(url, {
            data: JSON.stringify(body),
            headers: {
                "Accept": "*/*",
                "Content-Type": "application/json;charset=UTF-8"
            }
        })
    }

    static deleteModel<T>(url: string, params: any={},headers: any = {
        "Accept": "*/*",
        "Authorization": Token.getAuthorization()
    }):Promise<T> {
        return new Promise((resolve:any, reject:any) => {
            axios.delete(url,{
                params: params,
                headers: headers
            }).then((res: any) => {
                resolve(res.data);
            }).catch((e: any) => {
                reject();
            })
        });
    }

    static putModel<T>(url: string, body: any,headers: any = {
        "Accept": "*/*",
        "Authorization": Token.getAuthorization()
    }):Promise<T> {
        return new Promise((resolve:any, reject:any) => {
            axios.put(url,body,{
                headers: headers
            }).then((res: any) => {
                resolve(res.data);
            }).catch((e: any) => {
                reject();
            })
        });
    }
}
